import { Component, OnInit } from '@angular/core';
import { UserModel } from './usermodel/user.model';
import { Router } from '@angular/router';
import { UserInfo } from './usermodel/userinformation';
// import { SelectButtonModule } from 'primeng/primeng';
@Component({
    selector: 'login',
    templateUrl: 'login.component.html',
    styleUrls: ['login.component.scss']
})
export class LoginComponent implements OnInit {
    user: UserModel = new UserModel(null, null);
    // user: UserModel;
    languages: any = {
        label: 'CN',
        value: 'zh-cn',
    };
    language: string = 'zh-cn';
    constructor(private router: Router) { }

    ngOnInit() { }

    onSubmit() {
        if (this._validate()) {
            this.router.navigate(['/bookcatalogue']);
        }
    }

    private _validate(): boolean {
        let indexName = UserInfo.findIndex(authorised => authorised.name === this.user.name);
        let indexPassword = UserInfo.findIndex(authorised => authorised.password === this.user.password);

        if (indexName < 0 || indexPassword < 0) {
            // throw new Error("Username and Password do not match!");
            alert("Username and Password do not match!");

            return false;
        }
        return true;
    }
}